/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.test.beans;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.jbfilter.test.beans.util.ToStringUtils;


/**
 * @author Marcus Adrian
 *
 */
@Entity
public class Composer implements Cloneable {

	@Id
	//@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	private String firstName;
	private String particle;
	private String lastName;
	private Date born;
	private Date died;
	private Country country;
	@Transient
	private Set<Work> works = new HashSet<Work>();
	
	@Override
	public Composer clone() {
		Composer clone = new Composer();
		clone.setId(id);
		clone.setFirstName(firstName);
		clone.setParticle(particle);
		clone.setLastName(lastName);
		if (born != null) {
			clone.setBorn(new Date(born.getTime()));
		}
		if (died != null) {
			clone.setDied(new Date(died.getTime()));
		}
		clone.setCountry(country);
		for (Work work : works) {
			clone.addWork(work.clone());
		}
		return clone;
	}
	
	@Override
	public String toString() {
		ToStringBuilder builder = ToStringUtils.getToStringBuilderOneLine(this);
		builder.append("id", id);
	    builder.append("firstName", firstName);
	    builder.append("particle", particle);
	    builder.append("lastName", lastName);
	    builder.append("born", born);
	    builder.append("died", died);
	    builder.append("country", country);
	    for (Work work : works) {
			builder.append("work.title", work.getTitle());
		}
	    return builder.toString();
	}

	//adder
	public boolean addWork(Work work) {
		return works.add(work);
	}
	
	/**
	 * The century the composer is born.
	 * @return
	 */
	public int getCentury() {
		if (born == null) {
			return -1;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(born);
		int year = cal.get(Calendar.YEAR);
		return year/100 + 1;
	}
	
	public String getName() {
		StringBuilder builder = new StringBuilder();
		builder.append(firstName);
		builder.append(" ");
		if (particle != null) {
			builder.append(particle);
			builder.append(" ");
		}
		builder.append(lastName);
		return builder.toString();	
	}
	
	// simple getters and setters
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public Date getBorn() {
		return born;
	}
	public void setBorn(Date born) {
		this.born = born;
	}
	public Date getDied() {
		return died;
	}
	public void setDied(Date died) {
		this.died = died;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Country getCountry() {
		return country;
	}
	public void setCountry(Country country) {
		this.country = country;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getParticle() {
		return particle;
	}

	public void setParticle(String particle) {
		this.particle = particle;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((born == null) ? 0 : born.hashCode());
		result = prime * result + ((country == null) ? 0 : country.hashCode());
		result = prime * result + ((died == null) ? 0 : died.hashCode());
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		result = prime * result
				+ ((particle == null) ? 0 : particle.hashCode());
		result = prime * result + ((works == null) ? 0 : works.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Composer other = (Composer) obj;
		if (born == null) {
			if (other.born != null)
				return false;
		} else if (!born.equals(other.born))
			return false;
		if (country == null) {
			if (other.country != null)
				return false;
		} else if (!country.equals(other.country))
			return false;
		if (died == null) {
			if (other.died != null)
				return false;
		} else if (!died.equals(other.died))
			return false;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		if (particle == null) {
			if (other.particle != null)
				return false;
		} else if (!particle.equals(other.particle))
			return false;
		if (works == null) {
			if (other.works != null)
				return false;
		} else if (!works.equals(other.works))
			return false;
		return true;
	}
}
